<?php

namespace App\Http\Controllers\Stats\Risk;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Resources\Common\DataResource;
use App\Http\Resources\Common\DatasResource;
use App\Services\Stats\Risk\Slo\Elasticsearch\PlayerService;
use App\Services\Stats\Risk\Slo\Elasticsearch\GameService;
use App\Services\Stats\Risk\Slo\Elasticsearch\OrgGameService;
use App\Models\Admin\Stats\Risk\Config;

class SloController extends Controller
{

    /**
     * 获取配置
     */
    public function getConfig()
    {
        $config = Config::getCofing(Config::TYPE_SLO);

        return DataResource::make($config);
    }

    /**
     * 更新或创建配置
     */
    public function updateOrCreateConfig(Request $request)
    {
        $config = $request->validate([
            // 玩家
            'player.*.color' => 'required|string|size:7',
            'player.*.rtp.0' => 'required|string',
            'player.*.rtp.1' => 'required|numeric|between:0,100',
            'player.*.gain_player.0' => 'required|string',
            'player.*.gain_player.1' => 'required|numeric',
            // 游戏
            'game.*.color' => 'required|string|size:7',
            'game.*.rtp.0' => 'required|string',
            'game.*.rtp.1' => 'required|numeric|between:0,100',
            'game.*.gain_player.0' => 'required|string',
            'game.*.gain_player.1' => 'required|numeric',
            // 平台游戏
            'orgGame.*.color' => 'required|string|size:7',
            'orgGame.*.rtp.0' => 'required|string',
            'orgGame.*.rtp.1' => 'required|numeric|between:0,100',
            'orgGame.*.gain_player.0' => 'required|string',
            'orgGame.*.gain_player.1' => 'required|numeric'
        ]);

        Config::updateOrCreate([
            'type' => Config::TYPE_SLO,
        ], [
            'config' => $config,
        ]);

        return DataResource::make($config);
    }

    /**
     * 获取排行数据【玩家】
     */
    public function topPlayer(Request $request)
    {
        list($datas, $cresset) = PlayerService::make()->getTopList($request);

        return DatasResource::create($datas)->setAttribute('cresset', $cresset);
    }

    /**
     * 获取排行数据【游戏】
     */
    public function topGame(Request $request)
    {
        list($datas, $cresset) = GameService::make()->getTopList($request);

        return DatasResource::make($datas)->setAttribute('cresset', $cresset);
    }

    /**
     * 获取排行数据【平台游戏】
     */
    public function topOrgGame(Request $request)
    {
        list($datas, $cresset) = OrgGameService::make()->getTopList($request);

        return DatasResource::create($datas)->setAttribute('cresset', $cresset);
    }
}
